//pages/index/index.js
import { API_HOST } from "../../utils/const"
import http from "../../utils/http"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: API_HOST,
    cityList: [],
    cityName: '上海',
    cityCode: '310100',
    districtCode: '',
    blockCode: '',
    houseType: '',
    sortType: '',
    selAreaIds: '',
    rentPrice: '',
    isHideFilter: true,
    isScroll: true,
    pageNo: 1,
    totalPage: 1,
    list: [],
    currentReginText: '区域',
    currentTypeText: '类型',
    currentRankText: '默认排序',
    currentFilterText: '筛选'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.getPropertyListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    this.setData({
      cityCode: wx.getStorageSync('cityCode') || '310100',
      cityName: wx.getStorageSync('cityName') || '上海',
      pageNo: 1,
      list:[]
    });
    //console.log('onShow cityCode:', this.data.cityCode);
    this.getPropertyListData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },

  // goCity: function(){
  //   //this.resetFilter();
  //   wx.navigateTo({
  //     url: '/pages/index/cityList/cityList?code='+this.data.cityCode+'&name='+this.data.cityName
  //   });
  // },

  onMyEvent: function (e) {
    wx.stopPullDownRefresh();
    //console.log('======onMyEvent', e.detail.curParam, e.detail.curFilter);
    let that = this;
    let curFilter = e.detail.curFilter,
        curParam = e.detail.curParam;
    if(curFilter == 'RANK'){
      console.log('RANK RANK RANK');
      that.setData({
        sortType: curParam
      })
    } else if (curFilter == 'TYPE'){
      console.log('TYPE TYPE TYPE');
      that.setData({
        houseType: curParam
      })
    } else{
      console.log('REGIN BLOCK');
      that.setData({
        blockCode: curParam
      })
    }
    that.setData({
      isScroll: false,//true,
      pageNo: 1,
      list: []
    });
    that.getPropertyListData();
  },

  showFilter: function () {
    wx.stopPullDownRefresh();
    wx.hideTabBar({ animation: true })
    this.setData({
      isHideFilter: false,
      isScroll: false
    })
  },
  closeFilter: function () {
    wx.showTabBar({ animation: true }) 
    this.setData({
      isHideFilter: true,
      isScroll: true
    })
  },
  doFilter: function (e) {
    wx.showTabBar({ animation: true }) 
    // wx.hideTabBar({
    //   fail: function () {
    //     setTimeout(function () {
    //       wx.hideTabBar()
    //     }, 500)
    //   }
    // });
    //console.log('====doFilter e:',e);
    var selAreaIds = e.detail.selAreaIds;
    var rentPrice = e.detail.rentPrice;
    //console.log('====doFilter rentPrice:', rentPrice);
    this.setData({
      isHideFilter: true,
      isScroll: true,
      pageNo: 1,
      rentPrice: rentPrice,
      selAreaIds: selAreaIds,
      list: []
    });
    this.getPropertyListData();
  },
  resetFilter: function(e){
    //console.log('====resetFilter e:',e);
    this.setData({
      isHideFilter: true,
      isScroll: true,
      pageNo: 1,
      list: [],
      blockCode: '',
      houseType: '',
      sortType: '',
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.setData({
      pageNo: 1,
      list: []
    })
    this.getPropertyListData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //console.log('onReachBottom:',this.data.pageNo,this.data.totalPage);
    if (this.data.pageNo < this.data.totalPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      })
      this.getPropertyListData();
    } else {
      wx.showToast({
        title: "没有更多了",
        icon: "none"
      })
    }
  },

  getPropertyListData: function(){
    //console.log('====getPropertyListData', this.data.cityCode);
    var that = this;
    http({
      url: "/pre/list",
      method:'POST',
      data: {
        cityCode: that.data.cityCode,
        districtCode: '',
        blockCode: that.data.blockCode,
        houseType: that.data.houseType,
        sortType: that.data.sortType,
        areaFilter: that.data.selAreaIds,
        preAssessRent: that.data.rentPrice,
        pageNo: that.data.pageNo
        //,pageSize: 10
      },
      success: function (res) {
        console.log('====getPropertyListData success res:', res.data, 'totalPage:', res.totalPage, res.total);
        if (!res.data) return;
        console.log('before list:', that.data.list);
        var list = that.data.list;
        list = list.concat(res.data);
        that.setData({
          list: list,
          totalPage: res.totalPage//Math.ceil(res.total / 10),
        });
        console.log('after list:',list);
      },
      fail: function(res) {
        console.log('fail res:',res);
        // that.setData({
        //   list: [
        //     {
        //       'id': 116,
        //       'title': '杨浦区武东路280号厂房出租',
        //       'blockLabel': '武东路',
        //       'typeLabel': '工业用房',
        //       'totalArea': 234,
        //       'publishTime': '5-15',
        //       'minPrice': '31000',
        //       'maxPrice': '35000',
        //       'picUrl': ''
        //     },
        //     {
        //       'id': 117,
        //       'title': '普陀区桃浦镇真南路90㎡商铺出租',
        //       'blockLabel': '桃浦',
        //       'typeLabel': '商业用房',
        //       'totalArea': 80,
        //       'publishTime': '5-28',
        //       'minPrice': '9000',
        //       'maxPrice': '90000',
        //       'picUrl': ''
        //     },
        //     {
        //       'id': 123,
        //       'title': '杨浦区武东路280号住宅出租',
        //       'blockLabel': '武东路',
        //       'typeLabel': '住宅用房',
        //       'totalArea': 68,
        //       'publishTime': '10-27',
        //       'minPrice': '4500',
        //       'maxPrice': '5500',
        //       'picUrl': ''
        //     }
        //   ]
        // });
      }
    })
  }
  
})